<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="constants.StringConstants" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/edit-link.js"></script>
<link href="css/mystyle.css" rel="stylesheet" type="text/css">
<title>Food Management</title>
</head>
<body>
	<span style="float:left"><a href="/XMLProject/admin/admin-home.jsp">Go to Home</a></span><br/>
 	<span style="float:right">
		Hi, <strong>${sessionScope.ADMIN_USERNAME}</strong>-
		<a href="AccountServlet?action=<%=StringConstants.LOGOUT%>">Logout</a>
	</span>
	<h2>Food Management</h2>
	<c:set var="foodList" value="${requestScope.FOODLIST}"/>
	<c:set var="foodGroupList" value="${requestScope.FOODGROUPLIST}"/>
	<c:if test="${not empty foodList}">
		<table border="1">
			<thead>
            	<tr>
                   <th>No.</th>
                   <th>Name</th>
                   <th>FoodGroup</th>
                   <th>Edit</th>
                   <th>Delete</th>
               	</tr>
        	</thead>
        	<tbody>
        		<c:forEach var="foodDTO" items="${foodList}" varStatus="counter">
        		<tr>
        			<td>${counter.count}</td>
        			<form class="editForm" action="FoodServlet" method="post">
        			<td>
        				<input type="hidden" name="action" value="edit"/>
						<input type="hidden" name="id" value="${foodDTO.id}"/>
        				<input type="text" name="name" class="nameEdit" value="${foodDTO.name}"/>
        				<p class="error">Please input!!</p>
        			</td>
        			<td>
        				<select name="foodGroupId">
							<c:forEach var="item" items="${foodGroupList}">
								<c:set var="itemId" value="${item.id}"/>
								<c:set var="foodGroupId" value="${foodDTO.foodGroup.id}"/>
								<c:choose>
									<c:when test="${ itemId == foodGroupId}">
										<option value="${item.id}" selected="selected">${item.name}</option>
									</c:when>
									<c:when test="${ itemId != foodGroupId}">
										<option value="${item.id}">${item.name}</option>
									</c:when>
								</c:choose>
							</c:forEach>
						</select>
        			</td>
        			</form>
        			<td>
						<a href="#" onclick="clickLinkEdit(this)">Change</a>
					</td>
					<td>
						<form method="POST" action="FoodServlet">
						    <input type="hidden" name="action" value="delete">
						    <input type="hidden" name="id" value="${foodDTO.id}">
						    <a href="#" onclick="clickLinkDelete(this)">Delete</a>
						</form>
					</td>
        		</tr>
        		</c:forEach>
        	</tbody>
		</table>
	</c:if>
	<hr/>
	<form action="FoodServlet" method="post" onsubmit="return validateAdd()">
		Name: <input type="text" id="name" name="name" value=""/>
		<p id="errorAdd" class="error">Please input!!</p>
		<select name="foodGroupId">
			<c:forEach var="item" items="${foodGroupList}">
				<option value="${item.id}">${item.name}</option>
			</c:forEach>
		</select>
		<input type="submit" name="action" value="add"/>
	</form>
</body>
</html>